# encoding:utf-8

from base import read4csv,initialize_mt5,generate_data,get_rates4pos,conversion_time4find,convert_title,write_2csv
import pandas as pd
import time
import MetaTrader5 as mt5

symbol = 'XAUUSD'
level = 1440
# level = 30

# year=input("输入年份(默认2024)：")
# year=year if year != "" else "2024"
# month=input("输入月份(1,12)：")
# day=input("输入日期(0,30)：")
# hour=input("输入小时(0,12)：")
# minute=input("输入分钟(0,55)：")
# query_time = f'{year}/{month}/{day} {hour}:{minute}' #'1702424100'

# 输入要查询的时间
target_time = f'2024.01.03 06:03:38' # mt4时间，是GMT+3，时间戳是GMT，2024/9/19 23:00 1726786800

query_time=conversion_time4find(target_time,level)
query_timestamp = int(query_time.timestamp())
# print(df.columns)
# print(query_timestamp)
# print(df.tail(1))
try:
    title = convert_title(level)
    df = read4csv(symbol, level)
    # input(11)

    if None:
        if mt5.terminal_info() == None:
            initialize_mt5()
            print("执行初始化！")
        df2 = generate_data(get_rates4pos(symbol, level, 0, 5000))
        write_2csv(df2, symbol, level)
        time.sleep(1)
        df = read4csv(symbol,level)
    result = df[df['time'] == query_timestamp]
    # print(result.columns)
    high = "{:.5f}".format(result['close'].iloc[0])
    low = "{:.5f}".format(result['low'].iloc[0])
    BBANDS_Up = "{:.5f}".format(result['BBANDS_Up'].iloc[0])
    BBANDS_Low = "{:.5f}".format(result['BBANDS_Low'].iloc[0])
    tradetime= result['TradeTime'].iloc[0]
    print(high, '\t', low, '\t', BBANDS_Up, '\t', BBANDS_Low, '\n', tradetime)

except Exception as e:
    print("查询错误：", e)
# print(high, '\t', low, '\t', BBANDS_Up, '\t', BBANDS_Low,'\n', tradetime)

# dingding_push(f"### 提示：\n {high} {low} {BBANDS_Up} {BBANDS_Low}")

